package com.mzj.saas.mservice.qiyuesuo.service;

import com.qiyuesuo.sdk.v2.SdkClient;
import com.qiyuesuo.sdk.v2.bean.Employee;
import com.qiyuesuo.sdk.v2.bean.User;
import com.qiyuesuo.sdk.v2.json.JSONUtils;
import com.qiyuesuo.sdk.v2.request.EmployeeCreateRequest;
import com.qiyuesuo.sdk.v2.request.EmployeeListRequest;
import com.qiyuesuo.sdk.v2.request.EmployeeRemoveRequest;
import com.qiyuesuo.sdk.v2.request.EmployeeUpdateRequest;
import com.qiyuesuo.sdk.v2.response.EmployeeListResult;
import com.qiyuesuo.sdk.v2.response.SdkResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 契约锁接口-员工
 * 
 * @author shun
 * @createDate: 2021年6月22日 下午2:18:29
 * @version: 1.0
 *
 */
@Service
public class QiYueSuoEmployeeService {

	private Logger logger = LoggerFactory.getLogger(QiYueSuoEmployeeService.class);

	@Autowired
	private SdkClient sdkClient;

	/**
	 * 获取员工列表
	 * 
	 * @param tenantName
	 *            公司名称(必须与契约锁一致)
	 * @return
	 * @author shun
	 * @date 2021年6月22日
	 */
	public List<Employee> getEnterpriseEmployeeList(String tenantName) {
		EmployeeListRequest request = new EmployeeListRequest();
		request.setTenantName(tenantName);
		request.setSelectOffset(0);
		// 默认1000
		request.setSelectLimit(100);
		String response = sdkClient.service(request);
		SdkResponse<EmployeeListResult> responseObj = JSONUtils.toQysResponse(response, EmployeeListResult.class);
		if (responseObj.getCode() == 0) {
			EmployeeListResult result = responseObj.getResult();
			logger.info("员工查询，员工数量：{}", result.getTotalCount());
			return result.getList();
		} else {
			logger.info("员工查询失败，错误码:{}，错误信息:{}", responseObj.getCode(), responseObj.getMessage());
			return null;
		}
	}

	/**
	 * 添加员工
	 * 
	 * @param number
	 *            员工编号
	 * @param name
	 *            姓名
	 * @param mobile
	 *            电话
	 * @param tenantName
	 *            公司名称
	 * @return
	 * @author shun
	 * @date 2021年6月22日
	 */
	public String addEmployee(String name, String mobile, String tenantName) {
		if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(name)) {
			logger.info("创建员工，参数为空：mobile={}, name={}", mobile, name);
			return "签约用户数据有误";
		}
		User user = new User(name, mobile, "MOBILE");
		EmployeeCreateRequest request = new EmployeeCreateRequest();
		request.setTenantName(tenantName);
		request.setUser(user);
		String response = sdkClient.service(request);
		SdkResponse<Employee> responseObj = JSONUtils.toQysResponse(response, Employee.class);
		if (responseObj.getCode() == 0) {
			Employee result = responseObj.getResult();
			logger.info("创建员工，员工ID：{}", result.getId());
			return null;
		} else {
			logger.info("创建员工失败，错误码:{}，错误信息:{}", responseObj.getCode(), responseObj.getMessage());
			if(responseObj.getCode() == 1703){
				return "员工的姓名和实际姓名不匹配";
			}
			return responseObj.getMessage();
		}
	}

	/**
	 * 验证手机号码用户是否为员工
	 * 
	 * @param tenantName
	 * @param mobile
	 * @return
	 * @author shun
	 * @date 2021年6月22日
	 */
	public String validateAddEmployeeExists(String tenantName, String mobile, String userName) {
		if (StringUtils.isEmpty(tenantName) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(userName)) {
			return "签约用户数据有误";
		}

		List<Employee> list = getEnterpriseEmployeeList(tenantName);
		Employee entity = null;
		if (list != null) {
			for (Employee employee : list) {
				if (mobile.equals(employee.getMobile())) {
					entity = employee;
					break;
				}
			}
		}

		// 号码相同, 姓名不同
		if (entity != null && !entity.getName().equals(userName)) {
			// 更新
			return updateEmployee(userName, mobile, tenantName);
		} else if (entity == null) {
			// 如果不存在, 添加此用户
			return addEmployee(userName, mobile, tenantName);
		} else {
			return null;
		}
	}

	/**
	 * 删除员工
	 * 
	 * @param tenantName
	 * @param mobile
	 * @author shun
	 * @date 2021年6月22日
	 */
	public void deleteEmployee(String tenantName, String mobile) {
		User user = new User(mobile, "MOBILE");
		EmployeeRemoveRequest request = new EmployeeRemoveRequest(user);
		request.setTenantName(tenantName);
		String response = sdkClient.service(request);
		SdkResponse<Employee> responseObj = JSONUtils.toQysResponse(response, Employee.class);
		if (responseObj.getCode() == 0) {
			logger.info("移除员工成功, mobile={}, tenantName={}", mobile, tenantName);
		} else {
			logger.info("移除失败，错误码:{}，错误信息:{}", responseObj.getCode(), responseObj.getMessage());
		}
	}

	/**
	 * 更新员工信息
	 * 
	 * @param tenantName
	 * @param mobile
	 * @param userName
	 * @author shun
	 * @date 2021年6月22日
	 */
	public String updateEmployee(String name, String mobile, String tenantName) {
		User user = new User(name, mobile, "MOBILE");
		EmployeeUpdateRequest request = new EmployeeUpdateRequest();
		request.setUser(user);
		request.setTenantName(tenantName);
		String response = sdkClient.service(request);
		SdkResponse<Employee> responseObj = JSONUtils.toQysResponse(response, Employee.class);
		if (responseObj.getCode() == 0) {
			logger.info("更新员工成功, mobile={}, name={}, tenantName={}", mobile, name, tenantName);
			return null;
		} else {
			logger.info("更新失败，错误码:{}，错误信息:{}", responseObj.getCode(), responseObj.getMessage());
			return responseObj.getMessage();
		}
	}
}